home *** CD-ROM | disk | FTP | other *** search
/ Aminet 30 / Aminet 30 (1999)(Schatztruhe)[!][Apr 1999].iso / Aminet / dev / mui / bcc_src.lha / Parser / Str.cpp < prev    next >
C/C++ Source or Header  |  1998-03-15  |  2KB  |  131 lines

  1. #include "Str.h"
  2.  
  3. #define abs( a ) ( a > 0 ? a : -a )
  4.  
  5. StrBuf strbuf;
  6.  
  7. char *StrBuf::alloc( short size )
  8. {
  9.     short *p;
  10.     
  11.     if( size & 1 ) size++;
  12.     
  13.     p = b;
  14.     
  15.     while( 1 ) {
  16.         if( !*p ) break;
  17.         if( -*p >= size ) {
  18.             if( size == -*p - 2 ) size += 2;
  19.             if( -*p > size ) {
  20.                 p[ size/2 + 1 ] = *p + size + 2;
  21.                 break;
  22.             }
  23.             break;
  24.         } 
  25.         p += abs( *p )/2 + 1;
  26.         
  27.         if( p > b + STRBUF ) { printf( "String buffer overflow\n" ); exit( 10 ); }
  28.     
  29.     }
  30.     
  31.     if( !*p ) p[ 1 + size/2 ] = 0;
  32.     *p = size;
  33.     
  34.     p++;
  35.     
  36.     return (char*)p;
  37.     
  38.  
  39. }
  40.  
  41. void StrBuf::free( char *buf )
  42. {
  43.  
  44.     short *p = (short*)buf;
  45.     
  46.     if( !buf ) return;
  47.     
  48.     p--;
  49.     if( *p <= 0 ) { 
  50.         printf( "String alloc/dealloc error\n" ); 
  51.         exit( 10 ); 
  52.     }
  53.     
  54.     *p = -*p; 
  55.     
  56.     for( p = b; *p; ) {
  57.         if( *p < 0 ) {
  58.             if( !p[ ((-*p)>>1) + 1 ] ) { *p = 0; break; }
  59.             if( p[ ((-*p)>>1) + 1 ] < 0 ) *p = *p + p[ ((-*p)>>1) + 1 ] - 2;
  60.             else p += ((-*p)>>1) + 1;
  61.         }
  62.         else p += (*p>>1) + 1;
  63.     }
  64.  
  65. }
  66.  
  67.  
  68. String &String::operator+( char c ) { 
  69.         char *n;
  70.         n = (char*)malloc( Len+1 );
  71.         strcpy( n, str );
  72.         remstr();
  73.         n[Len] = c;
  74.         n[Len+1] = 0;
  75.         str = n;
  76.         Len++;
  77.         return *this;
  78. }
  79.  
  80. short String::Pos( char *t, short s )
  81. {
  82.     short r, l;
  83.     l = strlen( t );
  84.     
  85.     if( s >= 0 ) {
  86.         for( r = s; r < Len - l + 1; r++ ) if( !strncmp( str+r, t, l ) ) return r;
  87.         return -1;
  88.     } else {
  89.         for( r = Len-s; r >= 0; r-- ) if( !strncmp( str+r, t, l ) ) return r;
  90.         return -1;
  91.     }
  92.  
  93. }
  94.  
  95. void String::Cut( short p, short l )
  96. {
  97.  
  98.     char *n;
  99.     
  100.     if( !l ) l = strlen( str + p );
  101.     
  102.     n = strbuf.alloc( Len - l );
  103.     if( p ) memcpy( n, str, p );
  104.     strcpy( n + p, str + p + l );
  105.     
  106.     remstr();
  107.     str = n;
  108.     Len -= l;
  109.  
  110. }
  111.  
  112. char *String::operator+=( char *a )
  113. {
  114.     char *s;
  115.     
  116.     if( !str ) {
  117.         attach( a );
  118.         return str;
  119.     } else {
  120.         s = strbuf.alloc( Len + strlen( a ) );
  121.         strcpy( s, str );
  122.         strcpy( s+Len, a );
  123.         Len = strlen( s );
  124.         remstr();
  125.         str = s;
  126.     }
  127.     
  128.     return s;
  129. }
  130.  
  131.